<template>
  <div class="userMsg">
    <el-form
      :label-position="labelPosition"
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="80px"
      class="demo-ruleForm userMsg_form"
    >
      <el-form-item label="登录者">
        <el-input disabled v-model.number="userInfo.name"></el-input>
      </el-form-item>
      <el-form-item label="所属单位">
        <el-input disabled v-model.number="userInfo.hospital_name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >保存</el-button
        >
        <el-button @click="resetForm('ruleForm')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { editpassword } from '@/api/home.js'
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else if (value.length < 6) {
        callback(new Error('请输入不少于6位密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      userInfo: this.$store.state.user.info,
      labelPosition: 'right',
      formLabelAlign: {
        name: '',
        region: '',
        type: '',
      },
      ruleForm: {
        pass: '',
        checkPass: '',
        age: '',
      },
      rules: {
        pass: [{ validator: validatePass, trigger: 'blur' }],
        checkPass: [{ validator: validatePass2, trigger: 'blur' }],
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let aa = await editpassword({ password: this.ruleForm.pass })
          this.$message({
            type: 'success',
            message: aa.info,
          })
          this.$store.dispatch('user/logOut')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()

      this.$emit('showFn')
    },
  },
}
</script>

<style lang="less">
.userMsg {
  padding: 20px;
  .el-form-item {
    margin-bottom: 20px;
  }
  .el-form-item__label {
    font-size: 15px;
    color: #999;
  }
  .el-form-item__content {
    margin-left: 90px !important;
    font-size: 15px;
  }
  .userMsg_form {
    max-width: 500px;
    margin-bottom: 0px;
  }
}
</style>
